package com.panshi.controller.website;

import com.panshi.api.ILinksApi;
import com.panshi.po.LinksPO;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 友链链接管理
 */
@Controller
@Slf4j
public class LinksController {

    @Autowired
    ILinksApi iLinksApi;

    //进入友情链接页面
    @GetMapping("links/query")
    public String query() {
        return "website/links-query";
    }

    //友情链接查询
    @PostMapping("linksQuery")
    @ResponseBody
    public PageModel<LinksPO> likeQuery(PageModel<LinksPO> pm) {

        log.info("友链查询======{}", "*****************");

        pm.init();

        PageBeanLinksVo pageBeanVo = new PageBeanLinksVo();

        pageBeanVo.setCurrentPage(pm.getCurrentPage());

        pageBeanVo.setPageSize(pm.getPageSize());

        log.info("友链分页pageBeanVo============={}",pageBeanVo);

        log.info("PageBeanVo================={}", pageBeanVo);

        List<LinksPO> linksPOS = iLinksApi.queryLinksPostBody(pageBeanVo).getData();

        Integer str=iLinksApi.queryLinksNumber().getData();

        log.info("查询出来的数据====={}", linksPOS);

        pm.setList(linksPOS);

        pm.setCount(str);

        return pm;

    }

    @GetMapping("linksAdd")
    public String likeAdd() {
        return "website/links-add";
    }

    //友情链接增加
    @PostMapping("linksAdd")
    @ResponseBody
    public PageModel likeAdd(AddLinksVO addLinksVO) {

        log.info("前端传来的数据======{}", addLinksVO);

        PageModel pageModel = new PageModel();

        addLinksVO.setCreatedate(new Date());

        ResponseVO str = iLinksApi.addLinksPostBody(addLinksVO);

        Integer option= (Integer) str.getData();

        log.info("添加的返回来的参数==========={}",str);

        if (option == 1) {

            pageModel.setCode(10000);

            pageModel.setMsg(str.getMsg());

        } else if (option == 0) {

            pageModel.setCode(9999);

            pageModel.setMsg(str.getMsg());

        }
        return pageModel;
    }

    //模糊查询
    @PostMapping("linkDimQuery/{vague}")
    @ResponseBody
    public PageModel<LinksPO> dimQuery(PageModel<LinksPO> pm, @PathVariable String  vague){

        pm.init();

        log.info("模糊查询前端传来的数据============={}",vague);

        QueryDimLinksVO queryDimVO=new QueryDimLinksVO();

        queryDimVO.setSitesname(vague);

        queryDimVO.setCurrentPage(pm.getCurrentPage());

        queryDimVO.setPageSize(pm.getPageSize());

        List<LinksPO> list=iLinksApi.queryDimLinksPostBody(queryDimVO).getData();

        log.info("友情链接模糊查询出来的数据==============={}",list);

        Integer str=iLinksApi.queryDimLinksNumber(vague).getData();

        log.info("查询出来的条数============{}",str);

        pm.setList(list);

        pm.setCount(str);

        return pm;

    }

    //编辑页面
    @GetMapping("links-edit")
    public String edit(@RequestParam Integer id, Model model){
        log.info("编辑id==================={}",id);

        LinksPO linksPO=iLinksApi.queryId(id).getData();

        log.info("通过id查询出来的数据============{}",linksPO);

        model.addAttribute("linksPO",linksPO);

        return "website/links-edit";
    }

    //编辑修改
    @PostMapping("links-edit")
    @ResponseBody
    public Map<String,Object> edit(UpdateLinksVO updateLinksVO){

        log.info("修改前端传来的植====={}",updateLinksVO);

        Map<String,Object> map = new HashMap<>();

        updateLinksVO.setUpdatedate(new Date());

        ResponseVO str=iLinksApi.updateLinksPostBody(updateLinksVO);

        Integer option= (Integer) str.getData();

        log.info("option=============={}",option);

        log.info("修改返回来的数据============={}",str);

       if(option==0){

            map.put("success",false);

            map.put("message",str.getMsg());

        }else if(option==1){

            map.put("success",true);

            map.put("message",str.getMsg());
        }

        return  map;

    }

    //删除方法
    @PostMapping("links-del")
    @ResponseBody
    public Map<String,Object> del(DeleteLinksVO deleteLinksVO){

        log.info("删除传来的数据========{}",deleteLinksVO);

        Map<String,Object> map = new HashMap<>();

        deleteLinksVO.setUpdatedate(new Date());

        Integer state = iLinksApi.deleteLinksPostBody(deleteLinksVO).getData();

        if(state == 0){
            map.put("success",false);
            map.put("message","删除失败");
        }else if(state == 1){
            map.put("success",true);
            map.put("message","删除成功");
        }
        return map;

    }



}